<template>
  <el-dialog
    v-model="dialogVisible"
    title="查询生产订单"
    width="900px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="订单号">
            <el-input v-model="state.form.moCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货编码">
            <el-input v-model="state.form.invCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货名称">
            <el-input v-model="state.form.cInvName" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规格型号">
            <el-input v-model="state.form.cInvStd" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货代号">
            <el-input v-model="state.form.cInvAddCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制单人">
            <el-input v-model="state.form.createUser" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始日期">
            <el-date-picker
              v-model="state.form.queryDate"
              :clearable="false"
              range-separator="-"
              style="width: 100%"
              type="daterange"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产部门" required>
            <el-select
              v-model="state.form.mDeptCode"
              placeholder="部门"
              style="width: 100%"
            >
              <el-option
                v-for="dept in state.depts"
                :key="dept.deptCode"
                :label="dept.deptName"
                :value="dept.deptCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-row>
          <el-col :span="6" style="margin-left: 20px">
            <el-form-item label="是否同步" :span="12">
              <el-switch
                v-model="state.form.isSync"
                active-text="是"
                :active-value="true"
                inactive-text="否"
                :inactive-value="false"
                inline-prompt
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="query">查询</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { localStorageConfig } from '@/config/localstorage.config'
  import { getDeptListApi } from '@/api/base/dept/index'
  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  import dayjs from 'dayjs'
  const dialogVisible = ref(false)

  const state = reactive({
    form: {
      pkCorp: pkCorp,
      isSync: false,
      queryDate: [
        dayjs().subtract(300, 'day').format('YYYY-MM-DD'),
        dayjs().format('YYYY-MM-DD'),
      ],
      depts: [],
    },
  })

  const openDialog = () => {
    getDepts()
    dialogVisible.value = true
  }

  const getDepts = async () => {
    let params = {
      pkCorp: pkCorp,
      isProduce: 1,
    }
    const { code, data } = await getDeptListApi(params)
    if (code === 2000) {
      state.depts = data
    }
  }

  const query = () => {
    dialogVisible.value = false
    _emit('success-callback', state.form)
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
